<template>
  <div class="categories">
    <miAn-xie :betGao="betGao"></miAn-xie>
    <el-card>
      <!-- 添加 -->
      <el-button type="primary" @click="addNum">添加分类</el-button>
      <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input v-model="form.cat_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="父级分类" :label-width="formLabelWidth">
            <el-cascader
              v-model="form.cat_pid"
              :options="options"
              :props="addDefaultProps"
              clearable
            ></el-cascader>
            {{ form }}
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="categories_jueSe">确 定</el-button>
        </div>
      </el-dialog>
       <!-- 编辑 -->
       <el-dialog title="添加角色" :visible.sync="dialogForm">
          <el-form :model="Form">
            <el-form-item label="分类名称" :label-width="formLabelWidth">
              <el-input v-model="Form.cat_name" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="categories_bJi">确 定</el-button>
          </div>
        </el-dialog>
      <!-- 表格 -->
      <tree-table
        class="tree-tables"
        :data="spList"
        :columns="defaultProps"
        :selection-type="false"
        :expand-type="false"
        index-text="索引"
        :show-row-hover="false"
        show-index
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
        </template>
        <!-- 排序 -->
        <template slot="sorts" slot-scope="scope">
          <el-tag :type="scope.row.cat_level | time2">{{
            scope.row.cat_level | time1
          }}</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="caozuo" slot-scope="scope">
          <el-button type="primary"   @click="bj_categories(scope.row.cat_id)" size="mini">编辑</el-button>
          <el-button
            type="danger"
            @click="sc_categories(scope.row.cat_id)"
            size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[2, 3, 5, 6]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import {
  get_categories,
  post_categories,
  delete_categories,
  getS_categories,
  put_categories
} from "@/http/goods/categories";
export default {
  data() {
    return {
      // 面包屑
      betGao:[
        {
          'path':'/home',
          'title':'首页'
        },
        {
          'path':'/categories',
          'title':'商品管理'
        },
        {
          'path':'/categories',
          'title':'商品分类'
        },
      ],
      total: 0,
      query: {
        pagenum: 1,
        pagesize: 2,
      },
      // data中的数据
      defaultProps: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          // 表示当前列为模板列
          type: "template",
          // 表示当前这一列的模板名称
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "sorts",
        },
        {
          label: "操作",
          type: "template",
          template: "caozuo",
        },
      ],
      addDefaultProps: {
        value: "cat_id",
        label: "cat_name",
        checkStrictly: true,
      },
      spList: [],
      //   添加
      dialogFormVisible: false,
      formLabelWidth: "120px",
      form: {
        cat_name: "",
        cat_pid: "",
        cat_level: "",
      },
      options: [],
      // 查询  删除
      dataID: "",
      //   编辑
      dialogForm: false,
      Form: {
        cat_name: "",
        id: "",
      },
    };
  },
  mounted() {
    this.get_cae();
  },
  methods: {
    // 获取数据
    async get_cae() {
      let res = await get_categories(this.query);
      //   console.log(res);
      this.spList = res.data.result;
      this.total = res.data.total;
    },
    // 添加
    async addNum() {
      let res = await get_categories();
      console.log(res);
      this.options = res.data;
      this.dialogFormVisible = true;
    },
    // 确认添加
    async categories_jueSe() {
      let length = this.form.cat_pid.length;
      let cat_pid = length == 0 ? 0 : this.form.cat_pid[length - 1];
      let cat_level = length == 0 ? 0 : length - 1;
      let res = await post_categories({
        cat_name: this.form.cat_name,
        cat_pid: cat_pid,
        cat_level,
      });
      // console.log(res);
      this.dialogFormVisible = false;
      this.get_cae();
    },
    // 分页
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.query.pagesize = val;
      this.get_cae();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.query.pagenum = val;
      this.get_cae();
    }, 
    // 查询  删除
    async sc_categories(id) {
      let res = await getS_categories(id);
      //   console.log(res);
      this.dataID = res.data.cat_id;
      this.sc_categories(this.dataID);
    }, 
    async sc_categories(id) {
      let res = await delete_categories(id);
      //   console.log(res);
      this.get_cae();
    },

    // 查询编辑
    async bj_categories(id) {
        this.dialogForm = true;
      let res = await getS_categories(id);
        // console.log(res);
      this.Form.cat_name = res.data.cat_name;
      this.Form.id = res.data.cat_id;
    },
    async categories_bJi() {
      let res = await put_categories(this.Form.id,{cat_name:this.Form.cat_name})
    //   console.log(res);
      this.dialogForm = false;
      this.get_cae();
    },
  },
};
</script>
<style lang="scss">
.el-card {
  margin: 20px 0;
}
.tree-tables {
  margin: 20px 0;
}
.el-cascader-menu {
  height: 300px;
}
</style>